package queues;

import helpers.CrawlerHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tools.ant.taskdefs.condition.IsSet;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.appengine.api.taskqueue.TaskOptions.Method;

import models.AutoSiteModel;

import data.AutoSiteData;

public class QueueAutoSiteLastestNews extends HttpServlet {
	
	private static final Logger log = Logger.getLogger(QueueAutoSiteLastestNews.class.getName());
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		log.setLevel(Level.FINE);
		response.setContentType("text/plain");
		String url = (String) URLDecoder.decode(request.getParameter("url"));
		response.setContentType("text/plain");
		AutoSiteData aData = new AutoSiteData();
		AutoSiteModel autoSite = aData.getSite(url);
		if (autoSite != null) {
			//Get lastest news
			ArrayList<String> lastestNews = CrawlerHelper.getLastestNews(autoSite);
			for (int i = 0; i < lastestNews.size(); i++) {
				DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
				Queue queue = QueueFactory.getDefaultQueue();
				String urlCrawler = "/queue-crawler-news?siteUrl="+URLEncoder.encode(url)+"&queueid=" + URLEncoder.encode(lastestNews.get(i));
				log.info(urlCrawler);
				queue.add(TaskOptions.Builder.withUrl(urlCrawler).method(Method.GET));
			}
			
			//Update lastestScheduleSite
			autoSite.setLastestScheduleSite(new Date());
			aData.insert(autoSite);
			response.getWriter().println("Get list new success auto url:" + url);
		} else {
			response.getWriter().println("Get list new unsuccess auto url:" + url);
		}
	}
}
